<?php

namespace App\Http\Controllers\Admin;

use App\Services\WxMassCommentService;
use Illuminate\Http\Request;
use App\Services\WxMassService;

class WxMassCommentController extends BaseController
{
    private $wxMassCommentSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->wxMassCommentSer = new WxMassCommentService();
    }

    /**
     * 群发评论列表页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxMassArticle::with(['mass' => function ($massQuery) {
                $massQuery->where('msg_type', WxMassService::MSG_TYPE_NEWS);
                $massQuery->where('msg_id', '<>', 0);
                $massQuery->orderBy('mass_id', 'DESC');
            }])->select('*');
            //只限图文才有评论

            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('title', 'like', '%' . $keyword . '%');
            }
            $query->orderBy('mass_id', 'DESC');
            $query->orderBy('index_num', 'ASC');
            return $query->paginate($pageSize);
        }
        return view('admin.wxmasscomment.index', $this->viewData);
    }

    /**
     * 评论数据
     * @param Request $request
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function comment(Request $request)
    {
        if ($request->ajax()) {
            $msgDataId = $request->get('msg_data_id', 0);
            $articleIndex = $request->get('index', 0);
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxMassComment::with('user')->select('*');
            //只限图文才有评论
            $query->where('msg_data_id', $msgDataId);
            $query->where('index_num', $articleIndex);
            //时间范围
            $timeRange = $request->get('time_range', 'none');
            //评论类型
            $aroundCommentType = $request->get('around_comment_type', 0);
            //搜索关键字
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('around_content', 'like', '%' . $keyword . '%');
            }
            return $query->paginate($pageSize);
        }
    }

    /**
     * 设置评论关闭或打开
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function open(Request $request)
    {
        try {
            $massArtId = $request->input('massArtId', 0);
            $isOpen = (bool) $request->input('isOpen', 0);
            if (!$massArtId) {
                throw new \Exception('请指定评论文章');
            }
            if ($isOpen){
                $rs = $this->wxMassCommentSer->open($massArtId);
            }else{
                $rs = $this->wxMassCommentSer->close($massArtId);
            }
            if (!$rs) {
                throw new \Exception(($isOpen ? '打开' : '关闭') . '留言失败');
            }
            return $this->showMsg('success', ($isOpen ? '打开' : '关闭') . '留言成功');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 设置或取消留言精选
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function markelect(Request $request)
    {
        try {
            $commentId = $request->input('commentId', 0);
            $isMarkelect = (int) $request->input('isMarkelect', 0);
            if (!$commentId) {
                throw new \Exception('请指定评论文章');
            }
            if ($isMarkelect){
                $rs = $this->wxMassCommentSer->markelect($commentId);
            }else{
                $rs = $this->wxMassCommentSer->unmarkelect($commentId);
            }
            if (!$rs) {
                throw new \Exception(($isMarkelect ? '设置' : '取消') . '精选失败');
            }
            return $this->showMsg('success', ($isMarkelect ? '设置' : '取消') . '精选成功');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除评论
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function delete(Request $request)
    {
        try {
            $commentId = $request->input('commentId', 0);
            $rs = $this->wxMassCommentSer->delete($commentId);
            if (!$rs) {
                throw new \Exception('删除评论失败');
            }
            return $this->showMsg('success', '删除评论成功');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 回复留言
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function replyAdd(Request $request)
    {
        try {
            $commentId = $request->input('commentId', 0);
            $replyContent = $request->input('replyContent', '');
            $rs = $this->wxMassCommentSer->reply($commentId, $replyContent);
            if (!$rs) {
                throw new \Exception('回复留言失败');
            }
            return $this->showMsg('success', '回复留言成功');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除回复
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function replyDelete(Request $request)
    {
        try {
            $commentId = $request->input('commentId', 0);
            $rs = $this->wxMassCommentSer->replyDelete($commentId);
            if (!$rs) {
                throw new \Exception('删除回复失败');
            }
            return $this->showMsg('success', '删除回复成功');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
